package com.com.hemoWork.work1;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ClientSocketDemo {

    public static void main(String[] args) throws IOException {
        //创建与 ServerSocket连接的客户端Socket对象
        Scanner scanner = new Scanner(System.in);
        while (true) {
            Socket clientSocket = new Socket("192.168.137.1", 8088);
            //客户端发送数据给服务端  通过socket得到输出流
            System.out.println("请输出要发送给服务端的数据,输入exit表示不再往服务端发送数据了");
            String msg = scanner.next();
            OutputStream outputStream = clientSocket.getOutputStream();
            outputStream.write(msg.getBytes());
            if ("exit".equals(msg)) {
                break;
            }
            InputStreamReader inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
            char[] buffer = new char[1024];
            int len = 0;
            if ((len = inputStreamReader.read(buffer)) > 0) {
                String str = new String(buffer, 0, len);
                System.out.println("客户端接收数据：" + str);
            }
            outputStream.close();
            inputStreamReader.close();


        }

    }
}
